<?php

    /**
     * @package      Modules
     * @subpackage   Core_Controllers_Admin
     *
     * @license      GNU Lesser General Public Licence see LICENCE-LGPL file or http://www.gnu.org/licenses/lgpl.html
     */
    class Core_Controllers_Admin_Index extends Core_Controller_Action_Abstract
    {

	////////////////////////////////////////////////////////////////////////////
	//                                Variables                               //
	////////////////////////////////////////////////////////////////////////////

	    /**
	     * Parameters for plugins
	     *
	     * @var array
	     */
        protected $_pluginParams = array(
                                         '*' => array('auth.required' => true),
                                         '*' => array('acl.required' => false)
                                        );

        /**
         * Default namespace
         *
         * @var Zend_Session_Namespace
         */
        protected $_namespace = null;

        /**
         * Available modules list
         *
         * @var array
         */
        protected $_modules = array();

	////////////////////////////////////////////////////////////////////////////
	//                                 Methods                                //
	////////////////////////////////////////////////////////////////////////////

        /**
         * Constructor
         *
         * @param       Core_Controller_Request_Abstract     $request
         */
        public function __construct(Core_Controller_Request_Abstract $request)
        {
            parent::__construct($request);

            $this->_namespace			= new Zend_Session_Namespace('Default');
            $this->_namespace->errors	= array();
        }

        /**
         * Index
         *
         * @return       Core_Controller_Request_Abstract
         */
        public function index()
        {
            $rep = $this->_getResponse('http');
            $rep->setTemplate('main.tpl');

            $authNamespace  = Core_Authentification::getNamespace();
            $admin          = Doctrine_Query::create()
                                ->from('DbAdmin a')
                                ->leftJoin('a.DbLanguage l')
                                ->leftJoin('a.DbCountry c')
                                ->where('a.id = ?', $authNamespace->id)
                                ->fetchOne();
            
            // Local code used to load local files
            $adminLocalCode = ($admin != false) ? strtolower($admin->DbLanguage->code) . '_' . strtoupper($admin->DbCountry->code) : null;

            $this->_modules = array();
            $packagesList   = array();
            $modules        = array('base');
            $currentModule  = strtolower(Core_Context::get());

            $collection     = Doctrine_Query::create()
                            ->select('m.slug')
                            ->from('DbModule m')
                            ->execute(array(), Doctrine::HYDRATE_ARRAY);

            $authNamespace  = Core_Authentification::getNamespace();

            foreach ($collection as $item) {
                if (isset($authNamespace->authorized)) {
                    foreach ($authNamespace->authorized as $package) {
                        foreach ($package['modules'] as $slug => $module) {
                            $modules[] = $slug;

                            if (strtolower($slug) == strtolower($item['slug'])) {
                                $this->_modules[$package['package']->slug][$slug] = array('slug' =>$slug.'~index:index', 'display' => $module['display']);
                            }
                        }
                    }
                }
            }

            // Constructs packages' list
            foreach ($this->_modules as $packageSlug => $package) {
                $currentPackage             = array();
                $currentPackage['slug']     = $packageSlug;
                $currentPackage['name']     =  __('modules~package_' . $packageSlug, null, $adminLocalCode);
                $currentPackage['modules']  = array();

                foreach ($package as $moduleSlug => $module) {
                    if ($module['display'] === true) {
                        $currentModule                  = array();
                        $currentModule['slug']          = $moduleSlug;
                        $currentModule['text']          = __($moduleSlug . '~side_title', null, $adminLocalCode);
                        $currentModule['position']      = 0;
                        $currentModule['leaf']          = true;
                        $currentModule['cls']           = 'module';

                        $currentPackage['modules'][]    = $currentModule;
                    }
                }

                $packagesList[]         = $currentPackage;
            }
            
            $defaultNamespace = new Zend_Session_Namespace('Default');
            $rep->data->assign('languages',         Zend_Json::encode(DbCountryLanguage::getBackLanguages()));
            $rep->data->assign('packages',          Zend_Json::encode($packagesList));
            $rep->data->assign('locales',           Zend_Json::encode(Core_Locale::getInstance()->getBundles($modules, $adminLocalCode)));
            $rep->data->assign('preferences',       Zend_Json::encode(DbDisplayPreference::getValues($admin)));
            $rep->data->assign('session',           $authNamespace);
            $rep->data->assign('adminLocalCode',    $adminLocalCode);

            return $rep;
        }
    }